﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.Web.Components;
using System.Text;
using System.IO;
using Microsoft.Office.Interop.Word;


namespace CardLogistics.Web.MainPage.Ordering.Ordering_Cardinfo
{
    public partial class ListInfo : BasePage
    {
        private static readonly CardLogistics.DAL.Ordering.Ordering_CardInfoData dal = new DAL.Ordering.Ordering_CardInfoData();
        protected void Page_Load(object sender, EventArgs e)
        {
            WebUserControl_ToolBar1.DeleteEvent += new EventHandler(WebUserControl_ToolBar1_DeleteEvent);
            if (!IsPostBack)
            {
                BindDLL(ddlsupName, "basic_suppliers", "suppliersname", "suppliersid", " and STATUS=0 and ISDELETE=0");  //供应商
                BindDLL(ddlCardType, "basic_cardtype", "CARDTYPENAME", "ID", " and ISDELETE=0  ");//绑定卡类型
                BindDLL(ddlBtypename, "basic_basictype", "typename", "ID", " and typeno=0 and ISDELETE=0  ");//绑定芯片类型
                //状态 0 未提交 1 待审核 2 审核中 3 驳回 4 审核不通过 5审核通过
                BindDLL(ddlIsSubmit);
                BindRpt(LoadStrWhere);
            }
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void WebUserControl_ToolBar1_DeleteEvent(object sender, EventArgs e)
        {
            string delId = hdf_Id.Value;
            if (!string.IsNullOrEmpty(delId))
            {
                string[] ids = delId.TrimEnd(',').Split(',');
                string resStr = dal.Delete(ids[0]);

                BindRpt("");
            }
        }

        /// <summary>
        /// 绑定Rpt数据
        /// </summary>
        private void BindRpt(string strWhere)
        {
            int recordCount = 0;
            int pageCount = 0;
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append(" 1=1 ");
            if (txt_OrderNo.Text.Trim().Length > 0)
                sbStr.Append(" and OrderNo like '%" + txt_OrderNo.Text.Trim().ToUpper() + "%' ");//订单编号
            if (txt_BatchNo.Text.Trim().Length > 0)
                sbStr.Append(" and BatchNo like '%" + txt_BatchNo.Text.Trim().ToUpper() + "%' ");//批次号

            if (txt_StartOrderData.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(OrderData,'yyyy-MM-dd')>='" + txt_StartOrderData.Text.Trim() + "'");//订单开始日期
            if (txt_EndOrderData.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(OrderData,'yyyy-MM-dd')<='" + txt_EndOrderData.Text.Trim() + "'");//订单结束日期

            if (txt_StartGetDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(GetData,'yyyy-MM-dd')>='" + txt_StartGetDate.Text.Trim() + "'");//到货开始日期
            if (txt_EndGetDate.Text.Trim().Length > 0)
                sbStr.Append(" and to_char(GetData,'yyyy-MM-dd')<='" + txt_EndGetDate.Text.Trim() + "'");//到货结束日期

            if (txt_PConNo.Text.Trim().Length > 0)
                sbStr.Append(" and PConNo like '%" + txt_PConNo.Text.Trim() + "%' ");//合同编号
            if (ddlsupName.SelectedIndex != 0)
                sbStr.Append(" and SuppId='" + ddlsupName.Value + "'");//供应商
            if (ddlCardType.SelectedIndex != 0)
                sbStr.Append(" and cardtypeid='" + ddlCardType.Value + "'");//卡类型
            if (ddlBtypename.SelectedIndex != 0)
                sbStr.Append(" and basictypeid='" + ddlBtypename.Value + "'");//芯片类型
            //审核状态
            if (ddlIsSubmit.SelectedValue != "-1")
            {
                sbStr.Append(" and IsSubmit=" + ddlIsSubmit.SelectedValue);
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                sbStr.Append(strWhere);
            }
            System.Data.DataTable dt = CardLogistics.DAL.CommonData.QuickPage_Oracle("v_ordering_cardinfo", "ID", " issubmit,orderdata desc,orderno desc", AspNetPager1.PageSize, WebPageIndex, sbStr.ToString(), "*", out pageCount, out recordCount);
            SetNetPagerHtml(AspNetPager1, dt, rptList, recordCount);

            #region 导出数据
            //导出语句
            string strExpSql = "select ORDERNO 订单编号,BATCHNO 批次号,ORDERDATA 订单日期,GETDATA 到货日期,PConNo 合同编号,SuppliersName 供应商名称,cardtypename 卡类型,typename 芯片类型,STARTBOXNO 起始箱号,ENDBOXNO 结束箱号,STARTNO 起始卡号,ENDNO 结束卡号,CardTotal 总数量,PIC 订单负责人 from v_ordering_cardinfo where" + sbStr.ToString();
            if (!Page.IsClientScriptBlockRegistered("regExportSql"))
                Page.RegisterClientScriptBlock("regExportSql", "<script language=\"Javascript\">var qsql=\"" + Server.UrlEncode(strExpSql) + "\";</script>");
            #endregion
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            WebPageIndex = AspNetPager1.CurrentPageIndex;
            BindRpt("");
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            WebPageIndex = 1;
            BindRpt("");
        }
    }
}